<?php
namespace app\controller\marketingVideo;

use app\BaseController;
use app\service\marketVideo\M2vService;
use think\App;

class M2v extends BaseController
{
    protected $m2vService;

    public function __construct(App $app, M2vService $m2vService)
    {
        parent::__construct($app);
        $this->m2vService = $m2vService;
    }

    /**
     * 创建任务
     */
    public function createTask()
    {
        try {
            $params = $this->request->post();
            $user = $this->request->user;
            if (!$user) {
                return json(['code' => 401, 'msg' => '用户未登录']);
            }

            $result = $this->m2vService->createTask($user->uid, $params);
            return json(['code' => 200, 'msg' => '任务创建成功', 'data' => $result]);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 查询任务状态
     */
    public function queryTask()
    {
        try {
            $taskId = $this->request->param('taskId');
            if (!$taskId) {
                return json(['code' => 400, 'msg' => '任务ID不能为空']);
            }

            $user = $this->request->user;
            if (!$user) {
                return json(['code' => 401, 'msg' => '用户未登录']);
            }
            
            $result = $this->m2vService->queryTask($user->uid, $taskId);
            return json(['code' => 200, 'msg' => 'success', 'data' => $result]);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 获取任务列表
     */
    public function getTaskList()
    {
        try {
            $page = $this->request->param('page/d', 1);
            $pageSize = $this->request->param('pageSize/d', 10);
            $user = $this->request->user;
            if (!$user) {
                return json(['code' => 401, 'msg' => '用户未登录']);
            }

            $result = $this->m2vService->getTaskList($user->uid, $page, $pageSize);
            return json(['code' => 200, 'msg' => 'success', 'data' => $result]);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage()]);
        }
    }
} 